package com.example.springssotestclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping
public class HelloController {

    @Value("${sso.server.url}")
    String ssoUrl;

    @ResponseBody
    @GetMapping("/hello")
    public String hello(){

        return "hello";
    }

    @GetMapping("/employee")
    public String emplo(Model model, HttpSession session,String token){
        if (!StringUtils.isEmpty(token)){
            session.setAttribute("loginUser","aa");
        }
        Object loginUser = session.getAttribute("loginUser");
        if (loginUser == null) {
            return "redirect:"+ssoUrl+"?redirect_uri=http://client1.com:8081/employee";
        }

        List<String> list = new ArrayList<>();

        list.add("zx");
        list.add("xc");
        model.addAttribute("list",list);
        return "list";
    }
}
